﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Net.Cache;
using Yd.Util;
using Yd.Model;
using System.Data;
using System.Web.Script.Serialization;
using Yd.Domain.DAL;
using Yd.CommCls;
using Yd.BLL;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace yedian
{
    public partial class ActionAdd : AdminPage
    {
        public string imgUrl = "";
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            yd_action fnews = new yd_action();
            fnews.actionName = fNewsTitle.Value.Trim();
            fnews.actionContent = NewsContent.Value.Trim();
            fnews.actionAddress = this.actionAddress.Value.Trim();
            fnews.startTime = date1.Text.Trim();
            fnews.endTime = date2.Text;
            fnews.actionImgUrl = facePic.Value;
            decimal feiyong = 0;
            fnews.actionCost = feiyong;
            fnews.actionType = 1;
            fnews.shopid = 0;
            fnews.ticketInformation = actionTickSM.Value.Trim();
            string json=tickJson.Value.Trim();
            JavaScriptSerializer Serializer = new JavaScriptSerializer(); //实例化一个能够序列化数据的类
            List<yd_tick> tickList = Serializer.Deserialize<List<yd_tick>>(json);
            if (tickList==null||tickList.Count == 0) {
                errinfo.Text = "请先设置活动票";
                return;
            }
            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@actionType",1),
                new SqlParameter("@shopid",fnews.shopid),
                new SqlParameter("@actionName",fnews.actionName),
                new SqlParameter("@actionImgUrl",fnews.actionImgUrl),
                new SqlParameter("@actionContent",fnews.actionContent),
                new SqlParameter("@actionAddress",fnews.actionAddress),
                new SqlParameter("@actionCost",fnews.actionCost),
                new SqlParameter("@startTime",fnews.startTime),
                new SqlParameter("@endTime",fnews.endTime),
                new SqlParameter("@ticketInformation",fnews.ticketInformation)
            };
            //int _fresult = commdal.InstallAction(fnews);
            int _fresult = commdal.UpdateDataByPro("InsertAction", fparam);
            if (_fresult <= 0)
            {
                errinfo.Text = "添加" + fNewsTitle.Value.Trim() + "失败";
                return;
            }
            foreach (yd_tick model in tickList) {
                commdal.SaveOrUpdateSql(String.Format(@"INSERT INTO [dbo].[yd_ticket]([hdId],[ticketName],[fare],[endTime],[allVotes])VALUES({0},'{1}',{2},'{3}',{4})",_fresult,model.name,model.pice,model.time,model.num));
            }
            errinfo.Text = fNewsTitle.Value.Trim() + "添加成功";
            Response.Redirect("ActionList.aspx"); 
        }
        //protected void UploadButton_Click(object sender, EventArgs e)
        //{
        //    string uploadName = InputFile.Value;//获取待上传图片的完整路径，包括文件名 
        //    //string uploadName = InputFile.PostedFile.FileName; 
        //    string pictureName = "";//上传后的图片名，以当前时间为文件名，确保文件名没有重复 
        //    if (InputFile.Value != "")
        //    {
        //        int idx = uploadName.LastIndexOf(".");
        //        string suffix = uploadName.Substring(idx);//获得上传的图片的后缀名 
        //        pictureName = DateTime.Now.Ticks.ToString() + suffix;
        //    }
        //    try
        //    {
        //        if (uploadName != "")
        //        {
        //            string path = Server.MapPath("/Uploadfiles/Images/");
        //            imgUrl = "/Uploadfiles/Images/" + pictureName;
        //            fenmianPic.Value = "/Uploadfiles/Images/" + pictureName;
        //            InputFile.PostedFile.SaveAs(path + pictureName);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Response.Write(ex);
        //    }
        //}
        public bool IsNumeric(string value)
        {
            bool isDec = true;
            try
            {
                Convert.ToDecimal(value);
            }
            catch
            {
                isDec = false;
            }
            return isDec;
        }
    }
}